package com.example.anni.zhuzhoujia.tianya12345;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.anni.zhuzhoujia.R;
import com.shizhefei.view.indicator.IndicatorViewPager;
import com.shizhefei.view.indicator.ScrollIndicatorView;
import com.shizhefei.view.indicator.slidebar.ColorBar;
import com.shizhefei.view.indicator.transition.OnTransitionTextListener;
/**
 * Created by yangshengwen2016 on 2016/6/4.
 * 爆料
 */
public class BrokeFragment extends Fragment {
    private ViewPager mViewPager;
    private ScrollIndicatorView mScrollIndicatorView;
    private IndicatorViewPager mIndicatorViewPager;
    private String[] titles = {"全部爆料", "已审核爆料", "未审核爆料"};
    private Fragment[] fragments = {
            new BrokeFragmentWhole(),
            new BrokeFragmentMy(),
            new BrokeFragmentMyNo()
    };
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        LinearLayout root = (LinearLayout) inflater.inflate(R.layout.broke_fragment, null);
        return root;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //设置发布爆料点击事件
        ImageView fabu = (ImageView) view.findViewById(R.id.fabu);
        fabu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), BrokeEstablish.class);
                startActivity(intent);
            }
        });
        mViewPager = (ViewPager) view.findViewById(R.id.view_pager);
        mScrollIndicatorView = (ScrollIndicatorView) view.findViewById(R.id.indicator);
        mScrollIndicatorView.setScrollBar(
                new ColorBar(getActivity(), Color.RED, 2));

        mIndicatorViewPager = new IndicatorViewPager(
                mScrollIndicatorView, mViewPager);
        NewsAdapter adapter = new NewsAdapter(getChildFragmentManager());
        mIndicatorViewPager.setAdapter(adapter);

        //添加线条指定器
        mScrollIndicatorView.setScrollBar(new ColorBar(getActivity(), Color.RED, 5));
        //改变文字的颜色
        mScrollIndicatorView.setOnTransitionListener(new OnTransitionTextListener()
                .setColorId(getActivity(), R.color.colorAccent, R.color.black));

    }

    class NewsAdapter extends IndicatorViewPager.IndicatorFragmentPagerAdapter {

        public NewsAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);
        }

        @Override
        public int getCount() {
            return titles.length;
        }

        @Override
        public View getViewForTab(int i, View view, ViewGroup viewGroup) {
            //设置上选项卡标题
            TextView tvLabel = new TextView(getActivity());
            tvLabel.setText(titles[i]);
            tvLabel.setTextColor(0xFF000000);
            tvLabel.setBackgroundColor(0xFFFFFFFF);
            tvLabel.setPadding(20, 50, 20, 50);
            ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            tvLabel.setLayoutParams(lp);
            tvLabel.setGravity(Gravity.CENTER);
            return tvLabel;
        }

        @Override
        public Fragment getFragmentForPage(int i) {
            return fragments[i];
        }
    }
}
